﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wyn.PMS.Entity.Construction
{
    /// <summary>
    /// 账单编号生成器
    /// 形式：prefix - hotelId - yyMMdd-postfix
    /// <para>--------------------------</para>
    /// <para>@Author: cnalexi@gmail.com</para> 
    /// <para>@Create: 2012/3/21</para>  
    /// </summary>
    public class SerialGenerator
    {
        /*
         * length:       1      7         6         6
         * Bill_Debit : 'D' + HotelID + yyMMdd + 6位随机数
         * Bill_Credit: 'C' + HotelID + yyMMdd + 6位随机数
         */

        /// <summary>
        /// 产生随机尾数(2位)
        /// </summary>
        private static Random _random = new Random((int) DateTime.Now.Ticks);

        /// <summary>
        /// 循环增长ID
        /// </summary>
        private static int _idBase = 0;

        /// <summary>
        /// ID基数增长跨度
        /// </summary>
        private const int _idGap = 0x100;

        /// <summary>
        /// 主键长度
        /// </summary>
        public static readonly int _primaryKeyLength = 20;

        /// <summary>
        /// 产生一个16进制的数字字符串(前补零)
        /// </summary>
        /// <param name="length">需要的长度</param>
        /// <returns></returns>
        private static string CreateUniqueString(int length)
        {
            if (length <= 0) return string.Empty;

            _idBase = (_idBase + _idGap) % 0x0fffff00;

            int result = _idBase + _random.Next(0xff);

            string str = result.ToString("X").PadLeft(length, '0');

            return str.Length > length ? str.Substring(str.Length - length, length) : str;
        }

        /// <summary>
        /// 生成一个ID
        /// </summary>
        /// <param name="hotelId">酒店ID</param>
        /// <param name="prefix">前缀字母</param>
        /// <returns></returns>
        public static string CreateID(string hotelId,string prefix)
        {
            int tempLength = 0;

            if (!string.IsNullOrEmpty(hotelId))
                tempLength += hotelId.Length;

            if (!string.IsNullOrEmpty(prefix))
                tempLength += prefix.Length;

            return string.Format("{0}{1}{2:yyMMdd}{3}",
                prefix,
                hotelId,
                DateTime.Today,
                CreateUniqueString(_primaryKeyLength-tempLength-6));
        }

        /// <summary>
        /// 生成一个ID
        /// </summary>
        /// <param name="hotelId">酒店ID</param>
        /// <param name="prefix">前缀字母</param>
        /// <param name="keyTime">ID标记时间</param>
        /// <param name="pkLength">主键长度</param>
        /// <returns></returns>
        public static string CreateID(string hotelId,string prefix,DateTime keyTime,int pkLength)
        {
            int tempLength = 0;

            if (!string.IsNullOrEmpty(hotelId))
                tempLength += hotelId.Length;

            if (!string.IsNullOrEmpty(prefix))
                tempLength += prefix.Length;
            return string.Format("{0}{1}{2:yyMMdd}{3}",prefix,hotelId,keyTime,
                CreateUniqueString(pkLength-tempLength-6));
        }

    }
}
